<?php
namespace app\com\simtoon\app;

use app\com\simtoon\utils\EasemobUtils;
use Yii;
use app\com\simtoon\comm\Common;
use yii\base\Exception;
use app\com\simtoon\log\LogUtils;
use yii\data\ActiveDataProvider;
use yii\data\SqlDataProvider;
use app\com\simtoon\base\BaseModel;
use app\com\simtoon\utils\UserUtils;
use app\com\simtoon\utils\UserAuthUtils;
use app\com\simtoon\utils\CacheUtils;

header('Access-Control-Allow-Origin:*');
/**
 * 印刷系统接口
 * @author sxy
 *
 */
class SysUser extends BaseModel
{


  public static function tableName()
  {
    return 'supe_dev';
  }

    /**
     * 登录接口 2018.3.26 
     */
    public function Login($post)
    {
      //验证传入值
      $result = array();
      if(!isset($post['phone']) || empty($post['phone'])){
        $result['success'] = 1;
        $result['msg'] = "未指定登录名";
        return $result;
      }
      if(!isset($post['pwd']) || empty($post['pwd'])){
        $result['success'] = 1;
        $result['msg'] = "未指定密码";
        return $result;
      }

      // 查询结果集
      $pwd = md5($post['pwd']);
      $querySql = "SELECT u.uuid,u.pet_name,u.phone_number,c.clerk_name,c.clerk_id FROM t_sys_user u 
      left join t_sys_clerk c on u.clerk_id=c.clerk_id 
      where phone_number = '".$post['phone']."' and user_password = '".$pwd."'";
      LogUtils::info("Query sql={" . $querySql . "}", __METHOD__);
      $adp = new SqlDataProvider([
        'sql' => $querySql
        ]);
      $adp = $adp->getModels();

      //返回结果集
      if(count($adp) > 0){
        //把id转化为工号
        $adp[0]['uuid'] = str_pad($adp[0]['uuid'],4,"0",STR_PAD_LEFT);
        $result['success'] = 0;
        $result['msg'] = "登陆成功";
        $result['data'] = $adp[0];
        return $result;
      }else{
        $result['success'] = 2;
        $result['msg'] = "用户名或密码错误";
        return $result;
      }
    }

    /**
     * 修改密码接口 2018.3.26 
     */ 
    public function UpdatePwd($post)
    {
      //验证传入值
      $result = array();
      if(!isset($post['phone']) || empty($post['phone'])){
        $result['success'] = 1;
        $result['msg'] = "未指定手机号";
        return $result;
      }
      if(!isset($post['oldpwd']) || empty($post['oldpwd'])){
        $result['success'] = 1;
        $result['msg'] = "未指定旧密码";
        return $result;
      }
      if(!isset($post['newpwd']) || empty($post['newpwd'])){
        $result['success'] = 1;
        $result['msg'] = "未指定新密码";
        return $result;
      }
      //加密密码
      $oldpwd = md5($post['oldpwd']);
      $newpwd = md5($post['newpwd']);

      //验证旧密码是否正确
      $login = "select uuid from t_sys_user where phone_number='".$post['phone']."' and user_password='".$oldpwd."'";
      $login = $this->queryOne($login);

      //旧密码正确
      if($login){
        //更新数据
        $data['user_password'] = $newpwd;
        $res = $this->execUpdate('t_sys_user',$data,"phone_number='".$post['phone']."'");
      }
      //旧密码错误
      else{
        $result['success'] = 3;
        $result['msg'] = "旧密码错误";
        return $result;
      }

      // 成功
      if($res >=1){
        $result['success'] = 0;
        $result['msg'] = "更改成功";
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "更改失败";
        return $result;
      }
    }

    /**
     * 查询公司信息 2018.3.28 
     */ 
    public function SelClient($post)
    {
      
      //查询所有公司信息
      $sql = "select * from t_sys_client";
      $adp = $this->queryAll($sql);

      // 成功
      if($adp){
        $result['success'] = 0;
        $result['msg'] = "查询成功";
        $result['data'] = $adp;
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "查询失败";
        return $result;
      }
    }

    /**
     * 添加公司信息 2018.3.28 
     */ 
    public function InsClient($post)
    {

      //验证传入值
      $result = array();
      if(!isset($post['name']) || empty($post['name'])){
        $result['success'] = 1;
        $result['msg'] = "未指定公司名称";
        return $result;
      }
      if(!isset($post['conperson']) || empty($post['conperson'])){
        $result['success'] = 1;
        $result['msg'] = "未指定联系人";
        return $result;
      }
      if(!isset($post['tel']) || empty($post['tel'])){
        $result['success'] = 1;
        $result['msg'] = "未指定联系电话";
        return $result;
      }
      if(!isset($post['address']) || empty($post['address'])){
        $result['success'] = 1;
        $result['msg'] = "未指定公司地址";
        return $result;
      }
      
      //插入公司信息
      $data = array();
      $data['kh_name'] = $post['name'];
      $data['kh_address'] = $post['address'];
      $data['kh_conperson'] = $post['conperson'];
      $data['kh_tel'] = $post['tel'];

      $res = $this->execInsert('t_sys_client', $data);

      // 成功
      if($res >= 0){
        //定义密码
        $pwd = md5(123456);

        //获取当前时间
        date_default_timezone_set('PRC');
        $date = date('Y-m-d H-i-s');
        //插入用户信息
        $data = array();
        $data['address'] = $post['address'];
        $data['pet_name'] = $post['conperson'];
        $data['phone_number'] = $post['tel'];
        $data['clerk_id'] = 11;
        $data['user_password'] = $pwd;
        $data['user_time'] = $date;



        $res = $this->execInsert('t_sys_user', $data);

        if ($res >= 0) {
          $result['success'] = 0;
          $result['msg'] = "添加成功";
          return $result;
        }else{
          $result['success'] = 0;
          $result['msg'] = "添加成功";
          return $result;
        }
        
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "添加失败";
        return $result;
      }
    }

    /**
     * 录入订单 2018.3.28 
     */ 
    public function InsOrder($post)
    {

      //验证传入值
      $result = array();
      if(!isset($post['order_id']) || empty($post['order_id'])){
        $result['success'] = 1;
        $result['msg'] = "未指定订单号";
        return $result;
      }
      if(!isset($post['cp_name']) || empty($post['cp_name'])){
        $result['success'] = 1;
        $result['msg'] = "未指定产品名称";
        return $result;
      }
      if(!isset($post['cp_size']) || empty($post['cp_size'])){
        $result['success'] = 1;
        $result['msg'] = "未指定产品规格";
        return $result;
      }
      if(!isset($post['cp_num']) || empty($post['cp_num'])){
        $result['success'] = 1;
        $result['msg'] = "未指定产品数量";
        return $result;
      }
      if(!isset($post['cp_craft']) || empty($post['cp_craft'])){
        $result['success'] = 1;
        $result['msg'] = "未指定产品工艺";
        return $result;
      }
      if(!isset($post['kh_id']) || empty($post['kh_id'])){
        $result['success'] = 1;
        $result['msg'] = "未指定客户编号";
        return $result;
      }
      if(!isset($post['order_deadline']) || empty($post['order_deadline'])){
        $result['success'] = 1;
        $result['msg'] = "未指定交货日期";
        return $result;
      }
      if(!isset($post['order_person']) || empty($post['order_person'])){
        $result['success'] = 1;
        $result['msg'] = "未指定操作人ID";
        return $result;
      }
      if(!isset($post['order_payway']) || empty($post['order_payway'])){
        $result['success'] = 1;
        $result['msg'] = "未指定付款方式";
        return $result;
      }
      if(!isset($post['order_comment']) || empty($post['order_comment'])){
        $result['success'] = 1;
        $result['msg'] = "未指定备注信息";
        return $result;
      }

      //设置时区，获取当前时间
      date_default_timezone_set('PRC');
      $date = date('Y-m-d');
      
      //插入公司信息
      $data = array();
      $data['order_id'] = $post['order_id'];
      $data['cp_name'] = $post['cp_name'];
      $data['cp_size'] = $post['cp_size'];
      $data['cp_num'] = $post['cp_num'];
      $data['cp_craft'] = $post['cp_craft'];
      $data['cp_total'] = $post['cp_total'];
      $data['kh_id'] = $post['kh_id'];
      $data['order_deadline'] = $post['order_deadline'];
      $data['order_person'] = $post['order_person'];
      $data['order_payway'] = $post['order_payway'];
      $data['order_comment'] = $post['order_comment'];
      $data['create_time'] = $date;

      $res = $this->execInsert('t_sys_order', $data);

      // 成功
      if($res >= 0){
        $result['success'] = 0;
        $result['msg'] = "添加成功";
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "添加失败";
        return $result;
      }
    }

    /**
     * 查询订单列表 2018.3.28 
     */ 
    public function SelOrder($post)
    {
      //验证传入值
      $result = array();
      if(!isset($post['uid']) || empty($post['uid'])){
        $result['success'] = 1;
        $result['msg'] = "未指定操作人id";
        return $result;
      }

      //查询用户类型
      $sql = "select clerk_id from t_sys_user where uuid = '".$post['uid']."'";
      $clerk = $this->queryOne($sql);
      // var_dump($clerk);

      /////////////////////////////////////
      // 根据不同的类型查询对应的信息
      /////////////////////////////////////

      //如果是总裁办则返回所有订单信息
      if ($clerk['clerk_id'] == 1) {
        //查询所有订单信息
        $sql = "select o.*,c.*,u.pet_name,d.status from t_sys_order o 
        left join t_sys_client c on o.kh_id=c.kh_id 
        left join t_sys_user u on o.order_person=u.uuid
        left join t_dict_order d on o.order_status=d.id
        where o.order_status != 12 order by o.create_time desc";      
      }
      //如果是财务部，则返回财务订单
      else if ($clerk['clerk_id'] == 8) {
        $sql = "select o.*,c.*,u.pet_name,d.status from t_sys_order o 
        left join t_sys_client c on o.kh_id=c.kh_id 
        left join t_sys_user u on o.order_person=u.uuid
        left join t_dict_order d on o.order_status=d.id
        where o.order_status = 9 order by o.create_time desc";

      }
      //如果是生产部，则返回生产订单
      else if ($clerk['clerk_id'] == 3) {
        $sql = "select o.*,c.*,u.pet_name,d.status from t_sys_order o 
        left join t_sys_client c on o.kh_id=c.kh_id 
        left join t_sys_user u on o.order_person=u.uuid
        left join t_dict_order d on o.order_status=d.id
        where o.order_status = 4 order by o.create_time desc";
      }
      //如果是生产印前，则返回生产印前单
      else if ($clerk['clerk_id'] == 4) {
        $sql = "select o.*,c.*,u.pet_name,d.status from t_sys_order o 
        left join t_sys_client c on o.kh_id=c.kh_id 
        left join t_sys_user u on o.order_person=u.uuid
        left join t_dict_order d on o.order_status=d.id
        where o.order_status = 5 order by o.create_time desc";
      }
      //如果是生产印刷，则返回生产印刷单
      else if ($clerk['clerk_id'] == 5) {
        $sql = "select o.*,c.*,u.pet_name,d.status from t_sys_order o 
        left join t_sys_client c on o.kh_id=c.kh_id 
        left join t_sys_user u on o.order_person=u.uuid
        left join t_dict_order d on o.order_status=d.id
        where o.order_status = 6 order by o.create_time desc";
      }
      //如果是生产印后，则返回生产印后单
      else if ($clerk['clerk_id'] == 6) {
        $sql = "select o.*,c.*,u.pet_name,d.status from t_sys_order o 
        left join t_sys_client c on o.kh_id=c.kh_id 
        left join t_sys_user u on o.order_person=u.uuid
        left join t_dict_order d on o.order_status=d.id
        where o.order_status = 7 order by o.create_time desc";
      }
      //如果是质检部，则返回质检订单
      else if ($clerk['clerk_id'] == 7) {
        $sql = "select o.*,c.*,u.pet_name,d.status from t_sys_order o 
        left join t_sys_client c on o.kh_id=c.kh_id 
        left join t_sys_user u on o.order_person=u.uuid
        left join t_dict_order d on o.order_status=d.id
        where o.order_status = 8 order by o.create_time desc";
      }
      //如果是客户，则返回客户订单
      else if ($clerk['clerk_id'] == 11) {
        //验证传入值
        $result = array();
        if(!isset($post['phone']) || empty($post['phone'])){
          $result['success'] = 1;
          $result['msg'] = "未指定客户电话";
          return $result;
        }

        //根据电话查询客户ID
        $sql = "select kh_id from t_sys_client where kh_tel = '".$post['phone']."'";
        $res = $this->queryOne($sql);

        //根据客户ID查询客户订单
        $sql = "select o.*,c.*,u.pet_name,d.status from t_sys_order o 
        left join t_sys_client c on o.kh_id=c.kh_id 
        left join t_sys_user u on o.order_person=u.uuid
        left join t_dict_order d on o.order_status=d.id
        where o.kh_id = '".$res['kh_id']."' order by o.create_time desc";
      }
      //如果是销售部，则返回客户未确认订单
      // else if ($clerk['clerk_id'] == 2) {
      //   $sql = "select o.*,c.*,u.pet_name,d.status from t_sys_order o 
      //   left join t_sys_client c on o.kh_id=c.kh_id 
      //   left join t_sys_user u on o.order_person=u.uuid
      //   left join t_dict_order d on o.order_status=d.id
      //   where o.order_status = 1 or o.order_status = 3 order by o.create_time desc";
      // }
      //否则返回对应某个人的订单信息
      else{
        $sql = "select o.*,c.*,u.pet_name,d.status from t_sys_order o 
        left join t_sys_client c on o.kh_id=c.kh_id 
        left join t_sys_user u on o.order_person=u.uuid
        left join t_dict_order d on o.order_status=d.id
        where o.order_person = '".$post['uid']."' and o.order_status != 12 order by o.create_time desc";
      }
      
      //查询结果集
      $adp = $this->queryAll($sql);

      // 成功
      if($adp){
        $result['success'] = 0;
        $result['msg'] = "查询成功";
        $result['data'] = $adp;
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "查询失败";
        return $result;
      }
    }



    /**
     * 查询单价 2018.4.2 
     */ 
    public function SelPrice($post)
    {
      //验证传入值
      $result = array();
      if(!isset($post['model']) || empty($post['model'])){
        $result['success'] = 1;
        $result['msg'] = "未指定型号";
        return $result;
      }

      //查询所有公司信息
      $sql = "select price from t_sys_price where model = '".$post['model']."'";
      $adp = $this->queryOne($sql);


      // 成功
      if($adp){
        $result['success'] = 0;
        $result['msg'] = "查询成功";
        $result['data'] = $adp;
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "查询失败";
        return $result;
      }
    }

    /**
     * 确认订单 2018.4.2 
     */ 
    public function SureOrder($post)
    {
      //验证传入值
      $result = array();
      if(!isset($post['order_id']) || empty($post['order_id'])){
        $result['success'] = 1;
        $result['msg'] = "未指定订单号";
        return $result;
      }
      if(!isset($post['type']) || empty($post['type'])){
        $result['success'] = 1;
        $result['msg'] = "未指定确认类型";
        return $result;
      }

      //事务开始
      $transaction=$this->connection->beginTransaction();

      //根据确认类型执行内容
      switch ($post['type']) {
        // 客户确认
        case 'clientSure':
          $sql = 'UPDATE t_sys_order SET order_status = 2 WHERE order_id = "'. $post['order_id'].'"';
          $type = 1;
          break;

        // 客户驳回
        case 'clientRefuse':
          //验证驳回原因
          if(!isset($post['disreason']) || empty($post['disreason'])){
            $result['success'] = 1;
            $result['msg'] = "未指定驳回原因";
            return $result;
          }

          $sql = 'UPDATE t_sys_order SET order_status = 3,order_disreason = "'. $post['disreason'].'" WHERE order_id = "'. $post['order_id'].'"';
          $type = 1;
          break;

        // 财务确认首款
        case 'firstFinanceSure':
          $sql = 'UPDATE t_sys_order SET order_status = 4 WHERE order_id = "'. $post['order_id'].'"';
          $type = 1;
          break;

        // 印前确认
        case 'prePrintSure':
          $sql = 'UPDATE t_sys_order SET order_status = 6 WHERE order_id = "'. $post['order_id'].'"';
          $type = 1;
          break;

        // 印刷确认
        case 'printSure':
          $sql = 'UPDATE t_sys_order SET order_status = 7 WHERE order_id = "'. $post['order_id'].'"';
          $type = 1;
          break;

        // 印后确认
        case 'afterPrintSure':
          $sql = 'UPDATE t_sys_order SET order_status = 8 WHERE order_id = "'.   $post['order_id'].'"';
          $type = 1;
          break;

        // 质检确认
        case 'testSure':
          $sql = 'UPDATE t_sys_order SET order_status = 9 WHERE order_id = "'. $post['order_id'].'"';
          $type = 2;
          break;

        // 质检驳回
        case 'testRefuse':
          //改变订单到待生产确认
          $sql = 'UPDATE t_sys_order SET order_status = 4 WHERE order_id = "'. $post['order_id'].'"';
          $type = 4;
          break;

        // 财务确认尾款
        case 'lastFinanceSure':
          $sql = 'UPDATE t_sys_order SET order_status = 10 WHERE order_id = "'. $post['order_id'].'"';
          $type = 1;
          break;

        // 仓库确认
        case 'wareSure':
          $sql = 'UPDATE t_sys_order SET order_status = 11 WHERE order_id = "'. $post['order_id'].'"';
          $type = 3;
          break;
        
        default : 
          $sql = '';
          $type = 0;
          break;
      }

      //根据不同的类型改变订单状态
      $adp = $this->getCommand($sql)->execute();

      if($adp < 0){
        $transaction->rollBack();
        $result['success'] = 2;
        $result['msg'] = "订单未确认";
        return $result;
      }

      //根据传值执行不同操作
      if ($type == '1') {
        $transaction->commit();
        $result['success'] = 0;
        $result['msg'] = "订单已确认";  
        return $result;
      }
      //质检确认后增加库存
      else if ($type == '2') {
        //查询产品信息
        $sel = 'select order_id,cp_name,cp_size,cp_num,cp_craft,cp_total from t_sys_order where order_id = "'.$post['order_id'].'"';
        $adp = $this->queryOne($sel);

        //插入库存
        $int = 'INSERT INTO t_sys_ware (order_id,cp_name,cp_size,cp_num,cp_craft,cp_total) 
        VALUES ("'.$adp['order_id'].'","'.$adp['cp_name'].'","'.$adp['cp_size'].'","'.$adp['cp_num'].'","'.$adp['cp_craft'].'","'.$adp['cp_total'].'")';
        $res = $this->getCommand($int)->execute();

        //成功则返回数据
        if($res >= 0){
          $transaction->commit();
          $result['success'] = 0;
          $result['msg'] = "订单已确认";  
          return $result;
        }
        //失败
        else{
          $transaction->rollBack();
          $result['success'] = 2;
          $result['msg'] = "订单未确认";
          return $result;
        }
      }
      //仓库确认后减库存，更改耗材单状态
      else if ($type == '3') {  
        //更改耗材单状态
        $sql = 'UPDATE t_sys_supplies SET status = 1 WHERE order_id = "'. $post['order_id'].'"';
        $adp = $this->getCommand($sql)->execute();

        //减库存
        $del = 'DELETE FROM t_sys_ware WHERE order_id = "'. $post['order_id'].'"';
        $res = $this->getCommand($del)->execute();

        //成功则返回数据
        if($res >= 0){
          $transaction->commit();
          $result['success'] = 0;
          $result['msg'] = "订单已确认";  
          return $result;
        }
        //失败
        else{
          $transaction->rollBack();
          $result['success'] = 2;
          $result['msg'] = "订单未确认";
          return $result;
        }

      }
      //质检驳回后删除工序单和耗材单
      else if ($type == '4') {
        //删除工序单
        $step = 'DELETE FROM t_order_step WHERE order_id = "'. $post['order_id'].'"';
        $res = $this->getCommand($step)->execute();

        //成功
        if ($res >= 0) {
          //删除耗材单
          $supplies = 'DELETE FROM t_sys_supplies WHERE order_id = "'. $post['order_id'].'"';
          $res = $this->getCommand($supplies)->execute();
        }
        //失败
        else{
          $transaction->rollBack();
          $result['success'] = 2;
          $result['msg'] = "订单未确认";
          return $result;
        }    

        //成功则返回数据
        if($res >= 0){
          $transaction->commit();
          $result['success'] = 0;
          $result['msg'] = "订单已确认";  
          return $result;
        }
        //失败
        else{
          $transaction->rollBack();
          $result['success'] = 2;
          $result['msg'] = "订单未确认";
          return $result;
        }
      }

      //未指定参数则事务回滚
      $transaction->rollBack();
      $result['success'] = 2;
      $result['msg'] = "订单未确认";
      return $result;


    }

    /**
     * 查询设备信息 2018.4.8 
     */ 
    public function SelDevice($post)
    {

      //查询所有设备信息
      $sql = "select d.sb_id,d.sb_name,u.pet_name from t_sys_device d 
      left join t_sys_user u on d.sb_person = u.uuid";
      $adp = $this->queryAll($sql);

      //处理设备ID
      foreach ($adp as $key => $value) {
        $id = str_pad($value['sb_id'],4,"0",STR_PAD_LEFT);
        $adp[$key]['sb_id'] = $id;
      }

      // 成功
      if($adp){
        $result['success'] = 0;
        $result['msg'] = "查询成功";
        $result['data'] = $adp;
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "查询失败";
        return $result;
      }
    }

    /**
     * 添加设备信息 2018.4.8 
     */ 
    public function InsDevice($post)
    {

      //验证传入值
      $result = array();
      if(!isset($post['sb_id']) || empty($post['sb_id'])){
        $result['success'] = 1;
        $result['msg'] = "未指定设备ID";
        return $result;
      }
      if(!isset($post['name']) || empty($post['name'])){
        $result['success'] = 1;
        $result['msg'] = "未指定设备名称";
        return $result;
      }
      if(!isset($post['person']) || empty($post['person'])){
        $result['success'] = 1;
        $result['msg'] = "未指定负责人";
        return $result;
      }
      
      //插入设备信息
      $data = array();
      $data['sb_id'] = $post['sb_id'];
      $data['sb_name'] = $post['name'];
      $data['sb_person'] = $post['person'];

      $res = $this->execInsert('t_sys_device', $data);

      // 成功
      if($res >= 0){
        $result['success'] = 0;
        $result['msg'] = "添加成功";
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "添加失败";
        return $result;
      }
    }

    /**
     * 添加设备维修信息 2018.4.8 
     */ 
    public function InsDevRepair($post)
    {

      //验证传入值
      $result = array();
      if(!isset($post['sb_id']) || empty($post['sb_id'])){
        $result['success'] = 1;
        $result['msg'] = "未指定设备ID";
        return $result;
      }
      if(!isset($post['gz_details']) || empty($post['gz_details'])){
        $result['success'] = 1;
        $result['msg'] = "未指定故障现象";
        return $result;
      }

      $sql = 'select id from t_device_repair where sb_id ="'.$post['sb_id'].'" and gz_finished = 0';
      $adp = $this->queryOne($sql);

      if (!empty($adp)) {
        $result['success'] = 3;
        $result['msg'] = "该设备已在维修中";
        return $result;
      }
      

      //设置时区，获取当前时间
      date_default_timezone_set('PRC');
      $date = date('Y-m-d');

      //插入设备维修信息
      $data = array();
      $data['sb_id'] = $post['sb_id'];
      $data['create_time'] = $date;
      $data['gz_details'] = $post['gz_details'];
      if (!empty($post['gz_comment'])) {
        $data['gz_comment'] = $post['gz_comment'];
      }
      
      $res = $this->execInsert('t_device_repair', $data);

      // 成功
      if($res >= 0){
        $result['success'] = 0;
        $result['msg'] = "添加成功";
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "添加失败";
        return $result;
      }
    }

    /**
     * 查询设备维修信息 2018.4.8 
     */ 
    public function SelDevRepair($post)
    {

      //查询所有待设备维修信息
      $sql = "select r.sb_id,r.gz_details,r.gz_comment,r.create_time,d.sb_name,u.pet_name 
      from t_device_repair r 
      left join t_sys_device d on r.sb_id = d.sb_id
      left join t_sys_user u on d.sb_person = u.uuid
      where r.gz_finished = 0
      order by r.create_time desc";
      $adp = $this->queryAll($sql);

      //处理设备ID和备注信息
      foreach ($adp as $key => $value) {
        $id = str_pad($value['sb_id'],4,"0",STR_PAD_LEFT);
        $adp[$key]['sb_id'] = $id;

        if ($adp[$key]['gz_comment'] == null) {
          $adp[$key]['gz_comment'] = '';
        }
      }

      // 成功
      if($adp){
        $result['success'] = 0;
        $result['msg'] = "查询成功";
        $result['data'] = $adp;
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "查询失败";
        return $result;
      }
    }

    /**
     * 维修完成确认 2018.4.8 
     */ 
    public function SureDevRepair($post)
    {

      //验证传入值
      $result = array();
      if(!isset($post['sb_id']) || empty($post['sb_id'])){
        $result['success'] = 1;
        $result['msg'] = "未指定设备ID";
        return $result;
      }

      //更新维修单状态
      $data['gz_finished'] = 1;
      $res = $this->execUpdate('t_device_repair',$data,"sb_id='".$post['sb_id']."'");

      // 成功
      if($res>0){
        $result['success'] = 0;
        $result['msg'] = "确认成功";
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "确认失败";
        return $result;
      }
    }

    /**
     * 查询印前，印后，印刷，质检各个部门所有人员信息 2018.4.8 
     */ 
    public function SelPrintList($post)
    {

      //查询设备维修中负责人ID
      $sql = "select sb_person from t_sys_device where sb_status = 0";
      $person = $this->queryAll($sql);


      //查询所有印前人员信息
      $sql = "select u.uuid,u.phone_number,u.pet_name,u.user_head,u.user_time,u.address,u.sex,c.clerk_name 
      from t_sys_user u 
      left join t_sys_clerk c on u.clerk_id = c.clerk_id
      where u.clerk_id = 4";
      $prePrint = $this->queryAll($sql);

      //查询所有印后人员信息
      $sql = "select u.uuid,u.phone_number,u.pet_name,u.user_head,u.user_time,u.address,u.sex,c.clerk_name  
      from t_sys_user u 
      left join t_sys_clerk c on u.clerk_id = c.clerk_id
      where u.clerk_id = 5";
      $print = $this->queryAll($sql);

      //查询所有印刷人员信息
      $sql = "select u.uuid,u.phone_number,u.pet_name,u.user_head,u.user_time,u.address,u.sex,c.clerk_name  
      from t_sys_user u 
      left join t_sys_clerk c on u.clerk_id = c.clerk_id
      where u.clerk_id = 6";
      $afterPrint = $this->queryAll($sql);

      //查询所有质检人员信息
      $sql = "select u.uuid,u.phone_number,u.pet_name,u.user_head,u.user_time,u.address,u.sex,c.clerk_name  
      from t_sys_user u 
      left join t_sys_clerk c on u.clerk_id = c.clerk_id
      where u.clerk_id = 7";
      $test = $this->queryAll($sql);

      //取到设备维修中负责人ID
      foreach ($person as $key => $value) {
        $id = $person[$key]['sb_person'];

        //判断数组中有无负责人ID
        foreach ($prePrint as $key => $value) {
          //如果有负责人ID
          if ($prePrint[$key]['uuid'] == $id) {
            //释放该员工信息
            unset($prePrint[$key]);
          }
        }

        //判断数组中有无负责人ID
        foreach ($print as $key => $value) {
          //如果有负责人ID
          if ($print[$key]['uuid'] == $id) {
            //释放该员工信息
            unset($print[$key]);
          }
        }

        //判断数组中有无负责人ID
        foreach ($afterPrint as $key => $value) {
          //如果有负责人ID
          if ($afterPrint[$key]['uuid'] == $id) {
            //释放该员工信息
            unset($afterPrint[$key]);
          }
        }

        //判断数组中有无负责人ID
        foreach ($test as $key => $value) {
          //如果有负责人ID
          if ($test[$key]['uuid'] == $id) {
            //释放该员工信息
            unset($test[$key]);
          }
        }
      }

      //重构数组
      $prePrint = array_values($prePrint);
      $print = array_values($print);
      $afterPrint = array_values($afterPrint);
      $test = array_values($test);


      $adp['prePrint'] = $prePrint;
      $adp['print'] = $print;
      $adp['afterPrint'] = $afterPrint;
      $adp['test'] = $test;

      // 成功
      if($adp){
        $result['success'] = 0;
        $result['msg'] = "查询成功";
        $result['data'] = $adp;
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "查询失败";
        return $result;
      }
    }

    /**
     * 查询所有员工信息 2018.4.9 
     */ 
    public function UserInfoList($post)
    {


      //查询所有信息
      $sql = "select * 
      from t_sys_user u 
      left join t_sys_clerk c on u.clerk_id = c.clerk_id";
      $adp = $this->queryAll($sql);

      // 成功
      if($adp){
        $result['success'] = 0;
        $result['msg'] = "查询成功";
        $result['data'] = $adp;
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "查询失败";
        return $result;
      }
    }

    /**
     * 添加员工信息 2018.4.9 
     */ 
    public function InsUserInfo($post) 
    {

      //验证传入值
      $result = array();
      if(!isset($post['name']) || empty($post['name'])){
        $result['success'] = 1;
        $result['msg'] = "未指定用户昵称";
        return $result;
      }
      if(!isset($post['sex']) || empty($post['sex'])){
        $result['success'] = 1;
        $result['msg'] = "未指定性别";
        return $result;
      }
      if(!isset($post['clerk_id']) || empty($post['clerk_id'])){
        $result['success'] = 1;
        $result['msg'] = "未指定岗位ID";
        return $result;
      }
      if($post['clerk_id'] == 11){
        $result['success'] = 3;
        $result['msg'] = "不能添加客户信息，请移到添加客户接口";
        return $result;
      }
      if(!isset($post['phone_number']) || empty($post['phone_number'])){
        $result['success'] = 1;
        $result['msg'] = "未指定用户手机号";
        return $result;
      }
      if(!isset($post['address']) || empty($post['address'])){
        $result['success'] = 1;
        $result['msg'] = "未指定地址";
        return $result;
      }
      // if(!isset($post['emergency_contact']) || empty($post['emergency_contact'])){
      //   $result['success'] = 1;
      //   $result['msg'] = "未指定紧急联系人";
      //   return $result;
      // }
      // if(!isset($post['contact_phone']) || empty($post['contact_phone'])){
      //   $result['success'] = 1;
      //   $result['msg'] = "未指定紧急联系人电话";
      //   return $result;
      // }
      

      //设置时区，获取当前时间
      date_default_timezone_set('PRC');
      $date = date('Y-m-d');
      $pwd = md5(123456);

      //插入用户信息
      $data = array();
      $data['phone_number'] = $post['phone_number'];
      $data['user_password'] = $pwd;
      $data['user_time'] = $date;
      $data['pet_name'] = $post['name'];
      $data['clerk_id'] = $post['clerk_id'];
      $data['address'] = $post['address'];
      if (!empty($post['emergency_contact'])) {
        $data['emergency_contact'] = $post['emergency_contact'];
      }
      if (!empty($post['contact_phone'])) {
        $data['contact_phone'] = $post['contact_phone'];
      }
      
      $res = $this->execInsert('t_sys_user', $data);

      // 成功
      if($res >= 0){
        $result['success'] = 0;
        $result['msg'] = "添加成功";
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "添加失败";
        return $result;
      }
    }

    /**
     * 查询部门信息 2018.4.9 
     */ 
    public function ClerkList($post)
    {

      //查询所有部门信息
      $sql = "select * from t_sys_clerk ";
      $adp = $this->queryAll($sql);

      // 成功
      if($adp){
        $result['success'] = 0;
        $result['msg'] = "查询成功";
        $result['data'] = $adp;
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "查询失败";
        return $result;
      }
    }

    /**
     * 添加订单工序 2018.4.9 
     */ 
    public function InsOrderStep($post)
    {

      //验证传入值
      $result = array();
      if(!isset($post['order_id']) || empty($post['order_id'])){
        $result['success'] = 1;
        $result['msg'] = "未指定订单ID";
        return $result;
      }
      if(!isset($post['preprint_id']) || empty($post['preprint_id'])){
        $result['success'] = 1;
        $result['msg'] = "未指定印前人员ID";
        return $result;
      }
      if(!isset($post['afterprint_id']) || empty($post['afterprint_id'])){
        $result['success'] = 1;
        $result['msg'] = "未指定印后人员ID";
        return $result;
      }
      if(!isset($post['print_id']) || empty($post['print_id'])){
        $result['success'] = 1;
        $result['msg'] = "未指定印刷人员ID";
        return $result;
      }
      if(!isset($post['test_id']) || empty($post['test_id'])){
        $result['success'] = 1;
        $result['msg'] = "未指定质检人员ID";
        return $result;
      }
      
      //查询有没有对应的订单工序
      $sql = "select id from t_order_step where order_id ='".$post['order_id']."'";
      $adp = $this->queryOne($sql);

      //没有对应的工序单
      if (empty($adp)) {
        //插入工序单
        $data = array();
        $data['order_id'] = $post['order_id'];
        $data['preprint_id'] = $post['preprint_id'];
        $data['afterprint_id'] = $post['afterprint_id'];
        $data['print_id'] = $post['print_id'];
        $data['test_id'] = $post['test_id'];
        
        $res = $this->execInsert('t_order_step', $data);
      }
      //有对应的工序单
      else{
        $result['success'] = 3;
        $result['msg'] = "该定单已有工序单";
        return $result;
      }



      // 成功
      if($res >= 0){
        //查询有没有对应的耗材单
        $sql = "select id from t_sys_supplies where order_id ='".$post['order_id']."'";
        $adp = $this->queryOne($sql);

        //如果存在则改变订单状态为待印前确认
        if ($adp) {
          $sql = 'UPDATE t_sys_order SET order_status = 5 WHERE order_id = "'. $post['order_id'].'"';
          $res = $this->getCommand($sql)->execute();

          //成功
          if ($res >= 0) {
            $result['success'] = 0;
            $result['msg'] = "添加成功";
            return $result;
          }
          //失败
          else{
            $result['success'] = 2;
            $result['msg'] = "添加失败";
            return $result;
          }
        }
        //失败
        else{
          $result['success'] = 0;
          $result['msg'] = "添加成功";
          return $result;
        }
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "添加失败";
        return $result;
      }
    }

    /**
     * 添加耗材单 2018.4.9 
     */ 
    public function InsSupplies($post)
    {

      //验证传入值
      $result = array();
      if(!isset($post['order_id']) || empty($post['order_id'])){
        $result['success'] = 1;
        $result['msg'] = "未指定订单ID";
        return $result;
      }
      if(!isset($post['paper_type']) || empty($post['paper_type'])){
        $result['success'] = 1;
        $result['msg'] = "未指定纸张类型";
        return $result;
      }
      if(!isset($post['paper_size']) || empty($post['paper_size'])){
        $result['success'] = 1;
        $result['msg'] = "未指定纸张规格";
        return $result;
      }
      if(!isset($post['paper_num']) || empty($post['paper_num'])){
        $result['success'] = 1;
        $result['msg'] = "未指定纸张数量";
        return $result;
      }
      if(!isset($post['ink_type']) || empty($post['ink_type'])){
        $result['success'] = 1;
        $result['msg'] = "未指定油墨种类";
        return $result;
      }
      if(!isset($post['ink_num']) || empty($post['ink_num'])){
        $result['success'] = 1;
        $result['msg'] = "未指定油墨数量";
        return $result;
      }
      if(!isset($post['material_type']) || empty($post['material_type'])){
        $result['success'] = 1;
        $result['msg'] = "未指定材料品类";
        return $result;
      }
      if(!isset($post['material_num']) || empty($post['material_num'])){
        $result['success'] = 1;
        $result['msg'] = "未指定材料数量";
        return $result;
      }
      if(!isset($post['board_type']) || empty($post['board_type'])){
        $result['success'] = 1;
        $result['msg'] = "未指定板材品类";
        return $result;
      }
      if(!isset($post['board_num']) || empty($post['board_num'])){
        $result['success'] = 1;
        $result['msg'] = "未指定板材数量";
        return $result;
      }

      //查询有没有对应的耗材单
      $sql = "select id from t_sys_supplies where order_id ='".$post['order_id']."'";
      $adp = $this->queryOne($sql);

      //没有对应的耗材单
      if (empty($adp)) {
        //插入耗材单
        $data = array();
        $data['paper_type'] = $post['paper_type'];
        $data['paper_size'] = $post['paper_size'];
        $data['paper_num'] = $post['paper_num'];
        $data['ink_type'] = $post['ink_type'];
        $data['ink_num'] = $post['ink_num'];
        $data['material_type'] = $post['material_type'];
        $data['material_num'] = $post['material_num'];
        $data['board_type'] = $post['board_type'];
        $data['board_num'] = $post['board_num'];
        $data['order_id'] = $post['order_id'];
        
        $res = $this->execInsert('t_sys_supplies', $data);
      }
      //有对应的耗材单
      else{
        $result['success'] = 3;
        $result['msg'] = "该定单已有耗材单";
        return $result;
      }
        

      // 成功
      if($res >= 0){
        //查询有没有对应的工序单
        $sql = "select id from t_order_step where order_id ='".$post['order_id']."'";
        $adp = $this->queryOne($sql);

        //如果存在则改变订单状态为待印前确认
        if ($adp) {
          $sql = 'UPDATE t_sys_order SET order_status = 5 WHERE order_id = "'. $post['order_id'].'"';
          $res = $this->getCommand($sql)->execute();

          //成功
          if ($res >= 0) {
            $result['success'] = 0;
            $result['msg'] = "添加成功";
            return $result;
          }
          //失败
          else{
            $result['success'] = 2;
            $result['msg'] = "添加失败";
            return $result;
          }
        }
        // 失败
        else{
          $result['success'] = 0;
          $result['msg'] = "添加成功";
          return $result;
        }
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "添加失败";
        return $result;
      }
    }

    /**
     * 添加采购单 2018.4.9 
     */ 
    public function InsBuy($post)
    {

      //验证传入值
      if(!isset($post['price']) || empty($post['price'])){
        $result['success'] = 1;
        $result['msg'] = "未指定总价";
        return $result;
      }
      

      //插入设备维修信息
      $data = array();
      $data['paper_type'] = $post['paper_type'];
      $data['paper_size'] = $post['paper_size'];
      $data['paper_num'] = $post['paper_num'];
      $data['ink_type'] = $post['ink_type'];
      $data['ink_num'] = $post['ink_num'];
      $data['material_type'] = $post['material_type'];
      $data['material_num'] = $post['material_num'];
      $data['board_type'] = $post['board_type'];
      $data['board_num'] = $post['board_num'];
      $data['price'] = $post['price'];
      
      $res = $this->execInsert('t_sys_buy', $data);

      // 成功
      if($res >= 0){
        $result['success'] = 0;
        $result['msg'] = "添加成功";
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "添加失败";
        return $result;
      }
    }

    /**
     * 查询采购单列表 2018.4.9
     */ 
    public function BuyList($post)
    {
      //验证传入值
      $result = array();
      if(!isset($post['uid']) || empty($post['uid'])){
        $result['success'] = 1;
        $result['msg'] = "未指定操作人id";
        return $result;
      }

      //查询用户类型
      $sql = "select clerk_id from t_sys_user where uuid = '".$post['uid']."'";
      $clerk = $this->queryOne($sql);

      /////////////////////////////////////
      // 根据不同的类型查询对应的信息
      /////////////////////////////////////

      //如果是总裁办则返回所有订单信息
      if ($clerk['clerk_id'] == 1 || $clerk['clerk_id'] == 10) {
        //查询所有订单信息
        $sql = "select * from t_sys_buy";      
      }
      //如果是财务，则返回生产印前单
      else if ($clerk['clerk_id'] == 8) {
        $sql = "select * from t_sys_buy where status = 0";
      }

      
      //查询结果集
      $adp = $this->queryAll($sql);

      //处理订单状态
      foreach ($adp as $key => $value) {
        if ($value['status'] == 0) {
          $adp[$key]['status'] = "待财务确认";
        }else{
          $adp[$key]['status'] = "财务已确认";
        } 
      }

      // 成功
      if($adp){
        $result['success'] = 0;
        $result['msg'] = "查询成功";
        $result['data'] = $adp;
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "查询失败";
        return $result;
      }
    }

    /**
     * 查询采购单详情 2018.4.9 
     */ 
    public function BuyDetails($post)
    {
      //验证传入值
      $result = array();
      if(!isset($post['id']) || empty($post['id'])){
        $result['success'] = 1;
        $result['msg'] = "未指定订单号";
        return $result;
      }

      //查询订单详情
      $sql = "select * from t_sys_buy where id = '".$post['id']."'";
      $adp = $this->queryOne($sql);


      //处理订单状态
      if ($adp['status'] == 0) {
        $adp['status'] = "待财务确认";
      }else{
        $adp['status'] = "财务已确认";
      }

      // 成功
      if($adp){
        $result['success'] = 0;
        $result['msg'] = "查询成功";
        $result['data'] = $adp;
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "查询失败";
        return $result;
      }
    }

    /**
     * 查询出库单列表 2018.4.9 
     */ 
    public function OutWareList($post)
    {

      //查询订单详情
      $sql = "select o.*,c.*,u.pet_name,d.status from t_sys_order o 
        left join t_sys_client c on o.kh_id=c.kh_id 
        left join t_sys_user u on o.order_person=u.uuid
        left join t_dict_order d on o.order_status=d.id
        where order_status = 10";
      $adp = $this->queryAll($sql);

      // 成功
      if($adp){
        $result['success'] = 0;
        $result['msg'] = "查询成功";
        $result['data'] = $adp;
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "查询失败";
        return $result;
      }
    }

    /**
     * 根据order_id查询订单详情 2018.4.10 
     */ 
    public function ListDetails($post)
    {

      //验证传入值
      $result = array();
      if(!isset($post['order_id']) || empty($post['order_id'])){
        $result['success'] = 1;
        $result['msg'] = "未指定订单号";
        return $result;
      }

      //查询订单详情
      $sql = "select o.*,c.*,u.pet_name,d.status from t_sys_order o 
        left join t_sys_client c on o.kh_id=c.kh_id 
        left join t_sys_user u on o.order_person=u.uuid
        left join t_dict_order d on o.order_status=d.id
        where order_id='".$post['order_id']."'";
      $adp = $this->queryAll($sql);

      // 成功
      if($adp){
        $result['success'] = 0;
        $result['msg'] = "查询成功";
        $result['data'] = $adp;
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "查询失败";
        return $result;
      }
    }

    /**
     * 查询质检单列表 2018.4.10 
     */ 
    public function TestList($post)
    {

      //查询订单详情
      $sql = "select o.*,c.*,u.pet_name,d.status from t_sys_order o 
        left join t_sys_client c on o.kh_id=c.kh_id 
        left join t_sys_user u on o.order_person=u.uuid
        left join t_dict_order d on o.order_status=d.id
        where order_status = 8";
      $adp = $this->queryAll($sql);

      // 成功
      if($adp){
        $result['success'] = 0;
        $result['msg'] = "查询成功";
        $result['data'] = $adp;
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "查询失败";
        return $result;
      }
    }

    /**
     * 删除订单 2018.4.11 
     */ 
    public function DelOrder($post)
    {
      //验证传入值
      $result = array();
      if(!isset($post['type']) || empty($post['type'])){
        $result['success'] = 1;
        $result['msg'] = "未指定删除类型";
        return $result;
      }

      //根据类型删除信息
      switch ($post['type']) {
        //删除订单信息
        case 'delOrder':
          //验证传入值
          if(!isset($post['order_id']) || empty($post['order_id'])){
            $result['success'] = 1;
            $result['msg'] = "未指定删除类型";
            return $result;
          }

          //改变订单状态
          $data['order_status'] = 12;
          $res = $this->execUpdate('t_sys_order',$data,"order_id='".$post['order_id']."'");
          break;

        //删除员工信息
        case 'delUser':
          //验证传入值
          if(!isset($post['uid']) || empty($post['uid'])){
            $result['success'] = 1;
            $result['msg'] = "未指定员工编号";
            return $result;
          }

          //删除
          $res = $this->execDelete('t_sys_user',"uuid = '".$post['uid']."'");
          break;

        //删除客户信息
        case 'delClient':
          //验证传入值
          if(!isset($post['kh_id']) || empty($post['kh_id'])){
            $result['success'] = 1;
            $result['msg'] = "未指定客户编号";
            return $result;
          }

          //删除
          $res = $this->execDelete('t_sys_client',"kh_id = '".$post['kh_id']."'");
          break;

        //删除设备信息
        case 'delDevice':
          //验证传入值
          if(!isset($post['sb_id']) || empty($post['sb_id'])){
            $result['success'] = 1;
            $result['msg'] = "未指定客户编号";
            return $result;
          }

          //删除
          $res = $this->execDelete('t_sys_device',"sb_id = '".$post['sb_id']."'");
          break;
        
        default:
          # code...
          break;
      }

      

      // 成功
      if($res >= 0){
        $result['success'] = 0;
        $result['msg'] = "删除成功";
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "删除失败";
        return $result;
      }
    }

    /**
     * 查询耗材单列表 2018.4.11
     */ 
    public function SuppliesList($post)
    {

      //查询订单详情
      $sql = "select * from t_sys_supplies";
      $adp = $this->queryAll($sql);

      //处理订单状态
      foreach ($adp as $key => $value) {
        if ($value['status'] == 0) {
          $adp[$key]['status'] = "待仓库确认";
        }else{
          $adp[$key]['status'] = "仓库已确认";
        } 
      }

      // 成功
      if($adp){
        $result['success'] = 0;
        $result['msg'] = "查询成功";
        $result['data'] = $adp;
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "查询失败";
        return $result;
      }
    }

    /**
     * 财务确认采购单 2018.4.12 
     */ 
    public function SureBuy($post)
    {
      //验证传入值
      $result = array();
      if(!isset($post['id']) || empty($post['id'])){
        $result['success'] = 1;
        $result['msg'] = "未指定订单号";
        return $result;
      }

      //改变订单状态
      $data['status'] = 1;
      $res = $this->execUpdate('t_sys_buy',$data,"id='".$post['id']."'");

      // 成功
      if($res >= 0){
        $result['success'] = 0;
        $result['msg'] = "确认成功";
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "确认失败";
        return $result;
      }
    }

    /**
     * 查询原材料库存 2018.4.19
     */ 
    public function CountBuy($post)
    {

      //查询纸张库存
      $sql = "select paper_type,paper_size,sum(paper_num) as paper_num from t_sys_buy where status = 1 group by paper_type,paper_size";
      $paper_num = $this->queryAll($sql);

      //查询油墨库存
      $sql = "select ink_type,sum(ink_num) as ink_num from t_sys_buy where status = 1 group by ink_type";
      $ink = $this->queryAll($sql);

      //查询材料库存
      $sql = "select material_type,sum(material_num) as material_num from t_sys_buy where status = 1 group by material_type";
      $material = $this->queryAll($sql);

      //查询板材库存
      $sql = "select board_type,sum(board_num) as board_num from t_sys_buy where status = 1 group by board_type";
      $board = $this->queryAll($sql);

      //重构数据
      foreach ($paper_num as $key => $value) {
        $paper_num[$key]['type'] = '纸张';
      }

      foreach ($ink as $key => $value) {
        $ink[$key]['type'] = '油墨';
      }

      foreach ($material as $key => $value) {
        $material[$key]['type'] = '工艺材料';
      }

      foreach ($board as $key => $value) {
        $board[$key]['type'] = '板材';
      }

      //组合数据
      // $adp = array_merge($paper_num,$ink,$material,$board);
      $adp['paper'] = $paper_num;
      $adp['ink'] = $ink;
      $adp['material'] = $material;
      $adp['board'] = $board;

      // 成功
      if($adp){
        $result['success'] = 0;
        $result['msg'] = "查询成功";
        $result['data'] = $adp;
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "查询失败";
        return $result;
      }
    }

    /**
     * 查询产品库存 2018.4.19
     */ 
    public function SelWare($post)
    {

      //查询产品库存
      $sql = "select cp_name,cp_size,sum(cp_num) as cp_num,cp_craft from t_sys_ware group by cp_name,cp_size,cp_craft";
      $adp = $this->queryAll($sql);

      // 成功
      if($adp){
        $result['success'] = 0;
        $result['msg'] = "查询成功";
        $result['data'] = $adp;
        return $result;
      }
      //失败
      else{
        $result['success'] = 2;
        $result['msg'] = "查询失败";
        return $result;
      }
    }



    }
